#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define N 10
typedef struct student
{
     int sno;
     char sname[15];
}STU;

typedef struct msgbuf
{
     long mtype;
     STU stu[N];
}MSGBUF;



int main(int argc, char* argv[])
{
    int i = 0;
    MSGBUF mbf = {0};
    mbf.mtype = 1;
    int MSQID ;
    errno = 0;
    char *name = "./msgaaa";
    
    key_t key = ftok(name,0);
    MSQID = msgget(key, IPC_CREAT|0666);

    #if 0
    while(i < N)
    {

        msgrcv( MSQID, &mbf.stu[i], sizeof(mbf.stu[i].sname), 0, IPC_NOWAIT);

        
        if (-1 == errno)
        {
            perror("msgsnd");
            exit(-1);
        }
        printf(" %d -- %s\n", mbf.stu[i].sno, mbf.stu[i].sname);
        i++;
        sleep(1);
    }//while
    #endif

    while(1)
    {
        memset(&mbf,0x0,sizeof(mbf));
        int ret = msgrcv( MSQID, &mbf.stu[0], sizeof(mbf.stu[0].sname), 0, IPC_NOWAIT);

        printf("ret == %d \n", ret);
        
        if (-1 == errno)
        {
            perror("msgsnd");
            exit(-1);
        }
        printf(" %d -- %s\n", mbf.stu[0].sno, mbf.stu[0].sname);

        
        sleep(1);
    }
    
    
    msgctl(MSQID, IPC_RMID, NULL);
}






